﻿namespace OverStore.Exceptions
{
    using System;

    /// <summary>
    /// The exception that is thrown when trying to create instance of <see cref="T:OverStore.IPersistenceSession"/>
    /// using configuration contains invalid parameters.
    /// </summary>
    public class ConfigurationException<TComponent> : Exception
        where TComponent : class
    {
        /// <summary>
        /// Initializes new instance of <see cref="ConfigurationException{TComponent}"/> class.
        /// </summary>
        public ConfigurationException()
            : base()
        {
        }

        /// <summary>
        /// Initializes new instance of <see cref="ConfigurationException{TComponent}"/> class.
        /// </summary>
        public ConfigurationException(string message, Exception innerException)
            : base(message, innerException)
        { 
        }

        /// <summary>
        /// Initializes new instance of <see cref="ConfigurationException{TComponent}"/> class.
        /// </summary>
        public ConfigurationException(string message)
            : base(message)
        { 
        }

        /// <summary>
        /// Initializes new instance of <see cref="ConfigurationException{TComponent}"/> class.
        /// Parameter dummy introduced for resolve overload with other constructors.
        /// </summary>
        public ConfigurationException(TComponent component, string message)
            : base(message)
        {
            ConfigurationComponent = component;
        }

        /// <summary>
        /// Initializes new instance of <see cref="ConfigurationException{TComponent}"/> class.
        /// Parameter dummy introduced for resolve overload with other constructors.
        /// </summary>
        public ConfigurationException(TComponent component, string message, Exception innerException)
            : base(message, innerException)
        {
            ConfigurationComponent = component;
        }

        /// <summary>
        /// Get component causes error.
        /// </summary>
        public TComponent ConfigurationComponent { get; protected set; }
    }
}

namespace OverStore.Exceptions.Configuration
{
    using System;
    using System.Diagnostics;

    /// <summary>
    /// Provide single method allows to create new instance of <see cref="T:OverStore.Exceptions.ConfigurationException{TComponent}"/>.
    /// </summary>
    public static class ConfigurationExceptionExtension
    {
        /// <summary>
        /// Creates new instance of the <see cref="T:OverStore.Exceptions.ConfigurationException{TComponent}"/> class.
        /// </summary>
        public static ConfigurationException<TComponent> ConfigurationException<TComponent>(this TComponent component, string message)
            where TComponent : class
        {
            Debug.Assert(component != null, "Component is null.");
            Debug.Assert(!String.IsNullOrEmpty(message), "Message is null or empty string.");
            return new ConfigurationException<TComponent>(component, message);
        }

        /// <summary>
        /// Creates new instance of the <see cref="T:OverStore.Exceptions.ConfigurationException{TComponent}"/> class.
        /// </summary>
        public static ConfigurationException<TComponent> ConfigurationException<TComponent>(this TComponent component, string message, Exception innerException)
            where TComponent : class
        {
            Debug.Assert(component != null, "Component is null.");
            Debug.Assert(!String.IsNullOrEmpty(message), "Message is null or empty string.");
            Debug.Assert(innerException != null, "Inner exception is null.");
            return new ConfigurationException<TComponent>(component, message, innerException);
        }
    }
}
